package com.leyou.item.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/{id}")
    /**
     * 根据id查询
     */
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id){
        return ResponseEntity.ok(new CategoryDTO(this.categoryService.getById(id)));
    }
    /**
     * 根据分类id集合查询分类集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long>ids){
        return ResponseEntity.ok(CategoryDTO.convertEntityList(this.categoryService.listByIds(ids)));
    }

    /**
     * 根据父类目的id查询子类目的集合
     * @return
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") long pid){
        return ResponseEntity.ok(
               CategoryDTO.convertEntityList(this.categoryService.query().eq("parent_id",pid).list())
        );
    }
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByBrandId(@RequestParam("id") long id){
        return ResponseEntity.ok(this.categoryService.queryCategoryByBrandId(id));
    }

    //查询所有的一级目录以及对应的子目录
    @GetMapping("category")
    public ResponseEntity<List<Map<String, Object>>> findAllCategory() throws Exception {
        List<Map<String, Object>> allCategory = categoryService.findAllCategory();
        return  ResponseEntity.ok(allCategory);
    }
}
